package com.sheyu.mall.seckill.scheduled;

import com.sheyu.mall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author sheyu
 * @Desciption:
 * @date 2020/9/25 1:50 下午
 */

@Slf4j
@Service
public class SeckillScheduled {
    @Autowired
    private SeckillService seckillService;

    @Autowired
    RedissonClient redissonClient;

    private  final String uploadLock = "seckill:upload:lock";
    @Scheduled(cron = "0 0 1/1 * * ? ")
    public void uploadSeckillSkuLatest3Days() {
       // 1、重复上架无需处理

        RLock lock = redissonClient.getLock(uploadLock);
        try{
            lock.lock(10, TimeUnit.SECONDS);
            seckillService.uploadSeckillSkuLatest3Days();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
